package com.jdk.learn.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeUnit;

/**
 * 并发队列
 */
public class MultiThreadConcurrentLinkedDequeAndBlockingQueue{

    public static void main(String[] args) {
        //无界安全双端队列
        ConcurrentLinkedDeque<String> deque=new ConcurrentLinkedDeque<>();

        //offerLast 默认调用此方法追加到末尾
        deque.add("ricky");
        deque.add("sherry");

        deque.offer("ricky01");
        deque.offer("sherry02");

        System.out.println("size:"+deque.size());
        System.out.println(deque.poll());//先进先出  然后size-1
        System.out.println(deque.poll());//先进先出  然后size-1
        System.out.println("size:"+deque.size());
        System.out.println(deque.peek());//先进先出 然后size不变 只取值
        System.out.println(deque.peek());//先进先出 然后size不变 只取值
        System.out.println("size:"+deque.size());

        System.out.println("-----------------------------------------------");
        // 阻塞有界队列
        ArrayBlockingQueue abq=new ArrayBlockingQueue(3);
        abq.add("ricky");
        abq.add("sherry");
        abq.add("sherry01");
        try {
            System.out.println("阻塞中。。");
            //当队列满了时，给出插入的超时时间为5s，如果到时候没有则添加失败，即不添加
            boolean flag = abq.offer("ricky01", 5, TimeUnit.SECONDS);
            System.out.println("哎哟结束了，添加状态;"+flag);
            System.out.println(abq.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
